﻿#pragma once
#include <iostream>
using namespace std;

// 枚举颜色
enum Colour
{
	RED,
	BLACK
};

// 红黑树节点
template<class T>
struct RBTNode
{
	T _data;					// 存储值
	RBTNode<T>* _left;		// 左指针
	RBTNode<T>* _right;		// 右指针
	RBTNode<T>* _parent;     // 父亲指针

	// 颜色
	Colour _col;

	// 列表初始化
	RBTNode(const T& data)
		:_data(data)
		, _left(nullptr)
		, _right(nullptr)
		, _parent(nullptr)
	{}

};

template<class T, class Ref, class Ptr> // T是类型，Ref所传参数是引用，Ptr所传参数是指针
struct RBTreeIterator
{
	// RBTreeIterator封装到RBTNode内部是为了方便管理
	typedef RBTNode<T> Node;
	typedef RBTreeIterator<T, Ref, Ptr> Self;

	Node* _node;
	Node* _root; // 处理Iterator--传入参数end()是nullptr的情况
	RBTreeIterator(Node* node, Node* root)
		:_node(node)
		,_root(root)
	{}

	Self& operator++()
	{
		if (_node->_right)
		{
			// 右不为nullptr，下一个访问节点是右子树的最左节点
			Node* min = _node->_right;
			while (min->_left)
			{
				min = min->_left;
			}

			_node = min;
		}
		else
		{
			// 右为nullptr，访问祖先是父亲左的节点
			Node* cur = _node;
			Node* parent = cur->_parent;
			while (parent && cur == parent->_right) // parent不为nullptr,如果为nullptr走到了根节点
			{
				cur = parent;
				parent = cur->_parent;
			}

			_node = parent;
		}

		return *this;
	}

	Self& operator--()
	{
		if (_node == nullptr) // 传入end()
		{
			Node* rightNode = _root;
			while (rightNode && rightNode->_right) // 找到最右端节点 && 防止传入的_root节点就是nullptr节点
			{
				rightNode = rightNode->_right;
			}
			_node = rightNode;
		}
		else if (_node->_left) // 左子树不为nullptr
		{
			Node* rightNode = _node->_left;
			while (rightNode->_right) // 找到左子树的最右端节点
			{
				rightNode = rightNode->_right;
			}
			_node = rightNode;
		}
		else
		{
			// 左子树为空，找到当前节点属于祖父节点右子树的节点
			Node* cur = _node;
			Node* parent = cur->_parent;
			while (parent && cur == parent->_left)
			{
				cur = parent;
				parent = cur->_parent;
			}
			_node = parent;
		}
		return *this;
	}

	Ref operator*()
	{
		return _node->_data;
	}

	Ptr operator->()
	{
		return &_node->_data;
	}


	bool operator!= (const Self& s) const
	{
		return _node != s._node;
	}

	bool operator== (const Self& s) const
	{
		return _node == s._node;
	}
};


// 红黑树
template<class K, class T, class KeyOfT> // KeyOfT 所传参的是set/map中用于对比的值
class RBTree
{
	using Node = RBTNode<T>;
public:
	typedef RBTreeIterator<T, T&, T*> Iterator;
	typedef RBTreeIterator<T, const T&, const T*> ConstIterator;

	Iterator Begin()
	{
		Node* cur = _root;
		while (cur && cur->_left)
			cur = cur->_left;
		return Iterator(cur, _root);
	}

	Iterator End()
	{
		return Iterator(nullptr, _root);
	}

	ConstIterator Begin() const
	{
		Node* cur = _root;
		while (cur && cur->_left)
			cur = cur->_left;
		return ConstIterator(cur, _root);
	}

	ConstIterator End() const
	{
		return ConstIterator(nullptr, _root);
	}

	bool Insert(const T& data)
	{
		// 红黑树为空树
		if (_root == nullptr)
		{
			_root = new Node(data);
			_root->_col = BLACK;
			return true;
		}

		// 红黑树不为空树
		// 找到插入位置
		KeyOfT kot;
		Node* parent = nullptr;
		Node* cur = _root;
		while (cur)
		{
			if (kot(cur->_data) < kot(data))
			{
				parent = cur;
				cur = cur->_right;
			}
			else if (kot(cur->_data) > kot(data))
			{
				parent = cur;
				cur = cur->_left;
			}
			else {
				// 不实现冗余
				return false;
			}
		}

		// 创建新的节点
		cur = new Node(data);
		cur->_col = RED;
		// 连接孩子
		if (kot(parent->_data) < kot(data))
			parent->_right = cur;
		else
			parent->_left = cur;
		// 连接父亲
		cur->_parent = parent;

		// 调整红黑树的颜色
		// 如果插入后父亲为黑色节点直接返回true即可
		while (parent && parent->_col == RED)
		{
			Node* grandpa = parent->_parent;

			if (parent == grandpa->_left)
			{
				Node* uncle = grandpa->_right;
				if (uncle && uncle->_col == RED)
				{
					//     g
					//   p   u
					// 只需变色的情况
					// 该情况不需要考虑cur的位置
					parent->_col = uncle->_col = BLACK;
					grandpa->_col = RED;

					// 向上更新
					cur = grandpa;
					parent = cur->_parent;
				}
				else // uncle不存在或者，uncle的col为BLACK
				{
					// parent在grandpa左边并且cur在parent的左边
					//     g
					//   p   u
					// c
					if (cur == parent->_left)
					{
						// 右单旋+变色
						RotateR(grandpa);
						parent->_col = BLACK;
						grandpa->_col = RED;
					}
					else
					{
						// parent在grandpa左边并且cur在parent的右边
						//     g
						//   p   u
						//     c
						// cur == parent->_right
						// 左右双旋+变色
						RotateL(parent);
						RotateR(grandpa);
						// c变黑，g变红
						cur->_col = BLACK;
						grandpa->_col = RED;
					}
					break;
				}
			}
			else // parent == grandpa->_right
			{
				Node* uncle = grandpa->_left;
				if (uncle && uncle->_col == RED)
				{
					//     g
					//   u   p
					// 只需变色的情况
					// 该情况不需要考虑cur的位置
					parent->_col = uncle->_col = BLACK;
					grandpa->_col = RED;

					// 向上更新
					cur = grandpa;
					parent = cur->_parent;
				}
				else // uncle不存在或者，uncle的col为BLACK
				{
					// parent在grandpa右边并且cur在parent的右边
					//     g
					//   u   p
					//          c
					if (cur == parent->_right)
					{
						// 左单旋+变色
						RotateL(grandpa);
						parent->_col = BLACK;
						grandpa->_col = RED;
					}
					else
					{
						
						// parent在grandpa右边并且cur在parent的左边
						//     g
						//   u   p
						//     c
						// cur == parent->_left
						// 右左双旋+变色
						RotateR(parent);
						RotateL(grandpa);
						// c变黑，g变红
						cur->_col = BLACK;
						grandpa->_col = RED;
					}
					break;
				}
			}
		}
		// 根节点更新为BLACK
		_root->_col = BLACK;
		return true;
	}

	Iterator Find(const K& key)
	{
		
	}

private:

	// 右单旋
	void RotateR(Node* parent)
	{
		Node* subL = parent->_left;
		Node* subLR = subL->_right;

		parent->_left = subLR;
		if (subLR)
			subLR->_parent = parent;

		Node* pparent = parent->_parent;
		subL->_right = parent;
		parent->_parent = subL;

		if (pparent == nullptr)
		{
			_root = subL;
			subL->_parent = nullptr;
		}
		else
		{
			if (pparent->_left == parent)
				pparent->_left = subL;
			else
				pparent->_right = subL;
			subL->_parent = pparent;
		}
	}

	// 左单旋
	void RotateL(Node* parent)
	{
		Node* subR = parent->_right;
		Node* subRL = subR->_left;

		parent->_right = subRL;
		if (subRL)
			subRL->_parent = parent;

		Node* pparent = parent->_parent;
		subR->_left = parent;
		parent->_parent = subR;

		if (pparent == nullptr)
		{
			_root = subR;
			subR->_parent = nullptr;
		}
		else
		{
			if (pparent->_left == parent)
				pparent->_left = subR;
			else
				pparent->_right = subR;
			subR->_parent = pparent;
		}
	}

	Node* _root = nullptr;
};
